﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace XDCCManager.Controls
{
	/// <summary>
	/// Interaction logic for PackList.xaml
	/// </summary>
	public partial class PackList : UserControl
	{
		public PackList()
		{
			InitializeComponent();
		}

		public XDCCManager.Popups.PackEditor Editor
		{
			get { return (XDCCManager.Popups.PackEditor)GetValue(EditorProperty); }
			set { SetValue(EditorProperty, value); }
		}

		// Using a DependencyProperty as the backing store for Editor.  This enables animation, styling, binding, etc...
		public static readonly DependencyProperty EditorProperty =
			DependencyProperty.Register("Editor", typeof(XDCCManager.Popups.PackEditor), typeof(PackList), new UIPropertyMetadata(null, new PropertyChangedCallback((@do, args) =>
			{
				XDCCManager.Popups.PackEditor editor = args.OldValue as XDCCManager.Popups.PackEditor;
				if (editor != null)
				{
					editor.Remove -= (@do as PackList).editor_Remove;
					editor.Cancel -= (@do as PackList).editor_Cancel;
					editor.Ok -= (@do as PackList).editor_Ok;
				}
				editor = args.NewValue as XDCCManager.Popups.PackEditor;
				if (editor != null)
				{
					editor.Remove += (@do as PackList).editor_Remove;
					editor.Cancel += (@do as PackList).editor_Cancel;
					editor.Ok += (@do as PackList).editor_Ok;
				}
			})));

		private void editor_Ok(object sender, EventHandlers.PackEventArgs e)
		{
			Model.Packs.IPackList list = (FindResource("ServedFilesContextDataSource") as Model.ServedFilesContext).ViewPacks;
			Model.Actions.IAction action = list.EditPack(modifiedPack, e.Pack as Model.Packs.EditedPack);
			(FindResource("ActionQueueContextDataSource") as Model.ActionQueueContext).AddAction(action);
			Editor.Visibility = System.Windows.Visibility.Collapsed;
			modifiedPack = null;
		}

		private void editor_Cancel(object sender, EventArgs e)
		{
			Editor.Visibility = System.Windows.Visibility.Collapsed;
			modifiedPack = null;
		}

		private void editor_Remove(object sender, EventArgs e)
		{
			Model.Packs.IPackList list = (FindResource("ServedFilesContextDataSource") as Model.ServedFilesContext).ViewPacks;
			Model.Actions.IAction action = list.DeletePack(modifiedPack);
			(FindResource("ActionQueueContextDataSource") as Model.ActionQueueContext).AddAction(action);
			Editor.Visibility = System.Windows.Visibility.Collapsed;
			modifiedPack = null;
		}

		private void Edit_Click(object sender, RoutedEventArgs e)
		{
			if (Editor != null)
			{
				modifiedPack = FindPackItem(sender as DependencyObject);
				if (modifiedPack != null)
				{
					editPack = new Model.Packs.EditedPack() { Source = modifiedPack };
					editPack.Description = modifiedPack.Description;
					editPack.Note = modifiedPack.Note;
					Editor.DataContext = editPack;
					Editor.Visibility = System.Windows.Visibility.Visible;
				}
			}
		}

		private Model.Packs.IPack FindPackItem(DependencyObject element)
		{
			while ((element != null) && (!(element is ListBoxItem)))
			{
				element = VisualTreeHelper.GetParent(element);
			}
			if (element == null)
				return null;
			return (element as FrameworkElement).DataContext as Model.Packs.IPack;
		}

		private Model.Packs.IPack modifiedPack;
		private Model.Packs.EditedPack editPack;

		public Model.Packs.IPack SelectedPack
		{
			get
			{
				return list.SelectedItem as Model.Packs.IPack;
			}
			set
			{
				list.SelectedItem = value;
			}
		}

		private void list_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			IsSelectionEditable = (SelectedPack != null) && !(SelectedPack is Model.Packs.DeletedPack);
		}

		public bool IsSelectionEditable
		{
			get { return (bool)GetValue(IsSelectionEditableProperty); }
			set { SetValue(IsSelectionEditableProperty, value); }
		}

		// Using a DependencyProperty as the backing store for IsSelectionReadOnly.  This enables animation, styling, binding, etc...
		public static readonly DependencyProperty IsSelectionEditableProperty =
			DependencyProperty.Register("IsSelectionEditable", typeof(bool), typeof(PackList), new UIPropertyMetadata(false));
	}
}
